﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace SE_MWM_Creator
{
    public partial class Form1 : Form
    {
        string[] paths = new string[3];
        string pathsName="config";
        string pathToTool;
        bool toolFound;

        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string argTemp=null;

            argTemp += "/s:" + sourceFolderPath.Text;

            if (forceXML.Checked)
                argTemp += " /e";
            if (forceRebuild.Checked)
                argTemp += " /f";
            if (splitLogfile.Checked)
                argTemp += " /a";
            if (updateLogfile.Checked)
                argTemp += " /u";
            if (noCompare.Checked)
                argTemp += " /g";
            if (logFileEnabled.Checked && pathToLogfile.Text != "path to logfile")
                argTemp += " /l:" + "\"" + pathToLogfile.Text + "\"";


            label1.Text = argTemp;


            ProcessStartInfo ProcessInfo;
            Process proces;
            string output;
            Directory.GetCurrentDirectory();

            ProcessInfo=new ProcessStartInfo(pathToTool,argTemp);
            ProcessInfo.UseShellExecute = false;
            ProcessInfo.RedirectStandardOutput = true;
            ProcessInfo.CreateNoWindow = true;
            pathToTool = steamFolderPath.Text + "SteamApps\\common\\SpaceEngineers\\Tools\\MwmBuilder.exe";
            if (File.Exists(pathToTool))
            {
                richTextBox1.Text += "MwmBuilder.exe " + argTemp + "\n";
                proces = Process.Start(ProcessInfo);
                output = proces.StandardOutput.ReadToEnd();
                proces.WaitForExit();
                richTextBox1.Text += "Done. Your files are in the src folder.\n";
            }
            else
                MessageBox.Show("Are you completely sure you set the right path to Steam folder?\nIf yes do you have Space Engineers installed?\nIf you do then it,s an error on my part. Sorry!");
        }
        private void formClose(object sender, FormClosedEventArgs e)
        {

            File.AppendAllText("log.txt", richTextBox1.Text);
            if (paths != null) 
            {
                File.WriteAllLines(pathsName, paths);
            }
        }

        private void formLoad(object sender, EventArgs e)
        {
            if (File.Exists(pathsName))
            {
                toolFound = true;
                paths = File.ReadAllLines(pathsName);
                if (paths != null)
                {
                    steamFolderPath.Text = paths[1];
                    steamFolderPath.Show();
                    sourceFolderPath.Text = paths[0];
                    sourceFolderPath.Show();
                    pathToTool = steamFolderPath.Text + "SteamApps\\common\\SpaceEngineers\\Tools\\MwmBuilder.exe";
                    if (File.Exists(pathToTool))
                    {
                        toolFound = true;
                        label2.Text = "MwmBuilder.exe found";
                    }
                    else
                        label2.Text = "MwmBuilder.exe not found";
                }else
                    label2.Text = "Set the paths";
        }
            }

        private void steamPath(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            steamFolderPath.Show();
            steamFolderPath.Text = folderBrowserDialog1.SelectedPath + "\\";
            if(steamFolderPath.Text != null)
                paths[1] = steamFolderPath.Text;
            pathToTool = steamFolderPath.Text + "SteamApps\\common\\SpaceEngineers\\Tools\\MwmBuilder.exe";
            if (File.Exists(pathToTool))
            {
                toolFound = true;
                label2.Text = "MwmBuilder.exe found";
            }
            else
                label2.Text = "MwmBuilder.exe not found";
        }

        private void sourcePath(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            sourceFolderPath.Show();
            sourceFolderPath.Text = folderBrowserDialog1.SelectedPath + "\\";
            paths[0] = sourceFolderPath.Text;
        }

        private void LogfilePath_Click(object sender, EventArgs e)
        {
            logDialog.ShowDialog();
            pathToLogfile.Text = logDialog.FileName;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Process myProcess=new Process();
            myProcess.StartInfo.UseShellExecute = true;
            myProcess.StartInfo.FileName = "http://steamcommunity.com/id/MinerWars/myworkshopfiles/?section=guides&appid=244850";
            myProcess.Start();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Process myProcess = new Process();
            myProcess.StartInfo.UseShellExecute = true;
            myProcess.StartInfo.FileName = "https://code.google.com/p/gui-for-mwmbuilder/";
            myProcess.Start();
        }
    }
}
